package com.caipiao.crawler.lotteryNumber;

import com.alibaba.fastjson.JSON;
import com.caipiao.crawler.mapper.LotteryNumberMapper;
import com.caipiao.crawler.model.LotteryNumber;
import com.caipiao.util.DBTools;
import org.apache.ibatis.session.SqlSession;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;


/**
 * Created by adminitrator on 2018/7/28.
 */
public class LotteryNumberResultPipeline implements Pipeline {

    @Override
    public synchronized void process(ResultItems resultItems, Task task) {
        LotteryNumber m = resultItems.get("model");
        System.err.println("pipe process:" + Thread.currentThread().getId());
        if(m == null) {
            return;
        }
        commit(m);
    }

    public void commit(LotteryNumber model) {
//        System.err.println("commit");
//        System.err.println(JSON.toJSONString(model));
//        //TODO 写数据库，需要判断不存在的情况才写入
//        SqlSession session = DBTools.getSession();
//        LotteryNumberMapper mapper = session.getMapper(LotteryNumberMapper.class);
////        int count = mapper.selectCountByTerm(model.getTerm());
////        if(count==0) {
////            mapper.insertModel(model);
////        }
//        session.commit();
//        //TODO 如果第一次写入，触发更新判断是否中奖的逻辑
    }

}
